import { _decorator, Animation, Component, EventTouch, input, Input, Label, math, Node, Sprite } from 'cc';
import { GameManager } from './GameManager';
import { Constand } from './Constant';
import { AuidoManager } from './AuidoManager';
const { ccclass, property } = _decorator;

@ccclass('UiManager')
export class UiManager extends Component {
    private static instance: UiManager = null;
    
    @property(Node)
    public playerPlane: Node = null;
    //游戏开始界面
    @property(Node)
    public gameStart: Node = null;
    //游戏内界面
    @property(Node)
    public game: Node = null;
    //游戏结束界面
    @property(Node)
    public gameOver: Node = null;
    //游戏内分数
    @property(Label)
    public gameScore: Label = null;
    //结束界面分数
    @property(Label)
    public overScore: Label = null;
    //结算动画
    @property(Animation)
    public fightBoxSettlement: Animation = null;
    //血条
    @property(Sprite)
    public hp: Sprite = null;

    public static getInstance(): UiManager {
        return UiManager.instance;
    }
    protected onEnable(): void {
        UiManager.instance = this;
        input.on(Input.EventType.TOUCH_START, this._tochStart, this);
        input.on(Input.EventType.TOUCH_END, this._tochEnd, this);
        input.on(Input.EventType.TOUCH_MOVE, this._touchMove, this);
    }

    protected onDisable(): void {
        input.off(Input.EventType.TOUCH_START, this._tochStart, this);
        input.off(Input.EventType.TOUCH_END, this._tochEnd, this);
        input.off(Input.EventType.TOUCH_MOVE, this._touchMove, this);
    }

    start() {
        this.gameStart.active = true;
        this.game.active = false;
        this.gameOver.active = false;
    }

    update(deltaTime: number) {
    }

    _tochStart(event: EventTouch) {
        if (GameManager.getInstance().status == Constand.GameStatus.GAME) {
            GameManager.getInstance().isShooting(true);
        } else if(GameManager.getInstance().status == Constand.GameStatus.START) {
            this.gameStart.active = false;
            this.game.active = true;
            GameManager.getInstance().startGame();
            GameManager.getInstance().isShooting(true);
        }
    }

    _tochEnd(event: EventTouch) {
        if (GameManager.getInstance().status == Constand.GameStatus.GAME) {
            GameManager.getInstance().isShooting(false);
        }
    }

    _touchMove(event: EventTouch) {
        if (GameManager.getInstance().status == Constand.GameStatus.GAME) {
            const delta = event.touch.getDelta();
            let pos = this.playerPlane.position;
            let speed = GameManager.getInstance().speed;
            this.playerPlane.setPosition(pos.x + 0.001 * speed * delta.x, pos.y, pos.z - 0.001 * speed * delta.y);
        }
    }

    reStart(){
        this.game.active = true;
        this.gameOver.active = false;
        this.gameStart.active = false;
        GameManager.getInstance().reStart();
        AuidoManager.getInstance().play("button");
    }

    returnMenu() {
        this.game.active = false;
        this.gameOver.active = false;
        this.gameStart.active = true;
        GameManager.getInstance().reSet();
        AuidoManager.getInstance().play("button");
    }

    over(){
        this.game.active = false;
        this.gameOver.active = true;
        this.gameStart.active = false;
        this.overScore.string = GameManager.getInstance().score.toString();
        //播放动画
        this.fightBoxSettlement.play();
    }

    //更新分数
    updateScore(){
        this.gameScore.string = GameManager.getInstance().score.toString();
    }

    //更新血量
    updateHp(){
        //最大血量
        let hpMax = GameManager.getInstance().hpMax;
        //当前血量
        let hpCur = GameManager.getInstance().hpCurr;
        //最大血量转换为1
        let value = hpCur / hpMax;
        this.hp.fillRange = value;
    }
}


